package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Foodbatch;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.*;

@Repository("FoodbatchDAO")
@Transactional
public class FoodbatchDAOImpl extends AbstractJpaDao<Foodbatch> implements FoodbatchDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodbatch.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FoodbatchDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodbatch findFoodbatchById(Integer id) throws DataAccessException {

		return findFoodbatchById(id, -1, -1);
	}

	@Transactional
	public Foodbatch findFoodbatchById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchById", startResult, maxRows, id);
			return (Foodbatch) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByName(String name) throws DataAccessException {

		return findFoodbatchByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByNameContaining(String name) throws DataAccessException {

		return findFoodbatchByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByBatch(String batch) throws DataAccessException {

		return findFoodbatchByBatch(batch, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByBatch(String batch, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByBatch", startResult, maxRows, batch);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByBatchContaining(String batch) throws DataAccessException {

		return findFoodbatchByBatchContaining(batch, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByBatchContaining(String batch, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByBatchContaining", startResult, maxRows, batch);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByDate(Calendar date) throws DataAccessException {

		return findFoodbatchByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByDate(Calendar date, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByDate", startResult, maxRows, date);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByPrice(BigDecimal price) throws DataAccessException {

		return findFoodbatchByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByDescription(String description) throws DataAccessException {

		return findFoodbatchByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchByDescriptionContaining(String description) throws DataAccessException {

		return findFoodbatchByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@Transactional
	public Foodbatch findFoodbatchByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodbatchByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodbatch findFoodbatchByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchByPrimaryKey", startResult, maxRows, id);
			return (Foodbatch) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatch> findAllFoodbatchs() throws DataAccessException {

		return findAllFoodbatchs(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findAllFoodbatchs(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchs", startResult, maxRows);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatch> findFoodbatchsByDetectmethod(Integer detectmethodId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchsByDetectmethod", start, max, detectmethodId);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatch> findFoodbatchsByFood(Integer foodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchsByFood", start, max, foodId);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatch> findFoodbatchsByUnit(Integer unitId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchsByUnit", start, max, unitId);
		return new LinkedHashSet<Foodbatch>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatch> findFoodbatchByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodbatch>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodbatch> findFoodbatchsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodbatch.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Foodbatch> foodbatchs = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodbatchs;
	}

	@Transactional
	public Foodbatch findFoodbatchByPhysicalPrimaryKey(Integer foodId, Calendar date) throws DataAccessException {
		return findFoodbatchByPhysicalPrimaryKey(foodId, date, -1, -1);
	}

	@Transactional
	public Foodbatch findFoodbatchByPhysicalPrimaryKey(Integer foodId, Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchByPhyscialPrimaryKey", startResult, maxRows, foodId, date);
			return (Foodbatch) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodbatch entity) {
		return true;
	}
}
